#include "..\include\zaran_grid.h"
#include<fstream>
void ZaranGrid::SetGrid(Grid& background, MultiGrid& grid, const vector<int>& background_node_tag)
{
}
void ZaranGrid::SetInterNode()
{

	Grid& backGround = *GetBackgroundGrid();
	Grid& unstructGrid = *GetUnstructGrid();
	for (size_t iNode = 0; iNode < backGround.GetNodeNum(); ++iNode)
	{
		GridNode& local_node = *backGround.GetNode(iNode);
		double neibor_pool_radius = local_node.GetNeiborPoolRadius() * 1.1;
		if (*backGround.GetNodeType(iNode) != BoundType::interface)
			continue;
		for (size_t jNode = 0; jNode < unstructGrid.GetNodeNum(); ++jNode)
		{
			GridNode& search_node = *unstructGrid.GetNode(jNode);
			double dis = distance(*local_node.GetCoord(), *search_node.GetCoord());
			if (dis > neibor_pool_radius)
				continue;
			local_node.AddNeiborNode(unstructGrid.GetNode(jNode));
		}
	}


	for (size_t iNode = 0; iNode < unstructGrid.GetNodeNum(); ++iNode)
	{
		if (*unstructGrid.GetNodeType(iNode) == BoundType::solid_surface || *unstructGrid.GetNodeType(iNode) == BoundType::notbound)
			continue;
		GridNode& local_node = *unstructGrid.GetNode(iNode);
		double neibor_pool_radius = local_node.GetNeiborPoolRadius() * 1.1;
		for (size_t jNode = 0; jNode < backGround.GetNodeNum(); ++jNode)
		{
			GridNode& search_node = *backGround.GetNode(jNode);
			int node_tag = (*GetBackgroundGridNodeTagVec())[jNode];
			double dis = distance(*local_node.GetCoord(), *search_node.GetCoord());
			if (dis > neibor_pool_radius || node_tag == NOT_UPDATE_TAG)
				continue;
			local_node.AddNeiborNode(backGround.GetNode(jNode));
		}
	}

}
void ZaranGrid::OutGrid(std::ostream& fout)
{
	using std::endl;

	int outElementNum = 0;
	auto& backGround = *this->GetBackgroundGrid();
	auto& backGroundElement = *backGround.GetElement();
	auto& elementTag = *this->GetBackGroundGridElementTagVec();
	auto& nodeList = *backGround.GetNodeVec();
	for (size_t iElem = 0; iElem < backGroundElement.size(); ++iElem)
	{
		//if (elementTag[iElem] == STRUCT_ELEMENT_OUTSIDE_MODEL || elementTag[iElem] == STRUCT_ELEMENT_TRANSITION)
			outElementNum++;
	}
	fout << "variables=x,y" << endl;
	fout << "ZONE t=\"BackGround Grid \"" << endl;
	//fout << "solutiontime = " << time_.GetCurrentTime() << endl;
	fout << "N=" << nodeList.size() << ", E=" << outElementNum << ", F=FEPOINT, ET=QUADRILATERAL" << endl;
	for (size_t iNode = 0; iNode < nodeList.size(); ++iNode)
	{
		size_t iGrid = backGround.GetIndex();
		fout << nodeList[iNode].GetCoord()->x() << "  " << nodeList[iNode].GetCoord()->y() << endl;
	}

	for (size_t iElem = 0; iElem < backGroundElement.size(); ++iElem)
	{
		//if (elementTag[iElem] != STRUCT_ELEMENT_OUTSIDE_MODEL && elementTag[iElem] != STRUCT_ELEMENT_TRANSITION)
		//	continue;
		auto& elementNodeList = *backGroundElement[iElem].GetNodeIndexVec();

		for (size_t iNode = 0; iNode < elementNodeList.size(); ++iNode)
		{
			fout << elementNodeList[iNode] + 1 << "  ";
		}
		if (elementNodeList.size() == 3)
		{
			fout << elementNodeList[2] + 1;
		}
		fout << endl;
	}

	auto& unstructGrid = *this->GetUnstructGrid();
	auto& unstructNode = *unstructGrid.GetNodeVec();
	auto& unstrcutElement = *unstructGrid.GetElement();
	fout << "ZONE t=\"Unstruct Grid\"" << endl;
	//fout << "solutiontime = " << time_.GetCurrentTime() << endl;
	fout << "N=" << unstructNode.size() << ", E=" << unstrcutElement.size() << ", F=FEPOINT, ET=QUADRILATERAL" << endl;

	for (size_t iNode = 0; iNode < unstructNode.size(); ++iNode)
	{
		size_t iGrid = unstructGrid.GetIndex();
		fout << unstructNode[iNode].GetCoord()->x() << "  " << unstructNode[iNode].GetCoord()->y() << endl;
	}
	for (size_t iElem = 0; iElem < unstrcutElement.size(); ++iElem)
	{
		auto& elementNodeList = *unstrcutElement[iElem].GetNodeIndexVec();

		for (size_t iNode = 0; iNode < elementNodeList.size(); ++iNode)
		{
			fout << elementNodeList[iNode] + 1 << "  ";
		}
		if (elementNodeList.size() == 3)
		{
			fout << elementNodeList[2] + 1;
		}
		fout << endl;
	}
}